{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 012-pandas 中如何使用 string 方法？\n",
    "\n",
    "> How do I use string methods in pandas?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HELLO'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'hello'.upper()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "orders = pd.read_table('https://bit.ly/chips4cda')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 为什么使用 read_table\n",
    "\n",
    "\n",
    "# 什么是 tsv 文件？\n",
    "\n",
    "\n",
    "# 如何读取 tsv 文件？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>order_id</th>\n",
       "      <th>quantity</th>\n",
       "      <th>item_name</th>\n",
       "      <th>choice_description</th>\n",
       "      <th>item_price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Chips and Fresh Tomato Salsa</td>\n",
       "      <td>NaN</td>\n",
       "      <td>$2.39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Izze</td>\n",
       "      <td>[Clementine]</td>\n",
       "      <td>$3.39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Nantucket Nectar</td>\n",
       "      <td>[Apple]</td>\n",
       "      <td>$3.39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Chips and Tomatillo-Green Chili Salsa</td>\n",
       "      <td>NaN</td>\n",
       "      <td>$2.39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>Chicken Bowl</td>\n",
       "      <td>[Tomatillo-Red Chili Salsa (Hot), [Black Beans...</td>\n",
       "      <td>$16.98</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   order_id  quantity                              item_name  \\\n",
       "0         1         1           Chips and Fresh Tomato Salsa   \n",
       "1         1         1                                   Izze   \n",
       "2         1         1                       Nantucket Nectar   \n",
       "3         1         1  Chips and Tomatillo-Green Chili Salsa   \n",
       "4         2         2                           Chicken Bowl   \n",
       "\n",
       "                                  choice_description item_price  \n",
       "0                                                NaN     $2.39   \n",
       "1                                       [Clementine]     $3.39   \n",
       "2                                            [Apple]     $3.39   \n",
       "3                                                NaN     $2.39   \n",
       "4  [Tomatillo-Red Chili Salsa (Hot), [Black Beans...    $16.98   "
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "orders.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0                CHIPS AND FRESH TOMATO SALSA\n",
       "1                                        IZZE\n",
       "2                            NANTUCKET NECTAR\n",
       "3       CHIPS AND TOMATILLO-GREEN CHILI SALSA\n",
       "4                                CHICKEN BOWL\n",
       "                        ...                  \n",
       "4617                            STEAK BURRITO\n",
       "4618                            STEAK BURRITO\n",
       "4619                       CHICKEN SALAD BOWL\n",
       "4620                       CHICKEN SALAD BOWL\n",
       "4621                       CHICKEN SALAD BOWL\n",
       "Name: item_name, Length: 4622, dtype: object"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "orders.item_name.str.upper()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       False\n",
       "1       False\n",
       "2       False\n",
       "3       False\n",
       "4        True\n",
       "        ...  \n",
       "4617    False\n",
       "4618    False\n",
       "4619     True\n",
       "4620     True\n",
       "4621     True\n",
       "Name: item_name, Length: 4622, dtype: bool"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "orders.item_name.str.contains('Chicken')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "访问：\n",
    "https://pandas.pydata.org/pandas-docs/stable/reference/series.html#constructor\n",
    "\n",
    "搜索：\n",
    "string handling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0                                                     NaN\n",
       "1                                             Clementine]\n",
       "2                                                  Apple]\n",
       "3                                                     NaN\n",
       "4       Tomatillo-Red Chili Salsa (Hot), Black Beans, ...\n",
       "                              ...                        \n",
       "4617    Fresh Tomato Salsa, Rice, Black Beans, Sour Cr...\n",
       "4618    Fresh Tomato Salsa, Rice, Sour Cream, Cheese, ...\n",
       "4619    Fresh Tomato Salsa, Fajita Vegetables, Pinto B...\n",
       "4620     Fresh Tomato Salsa, Fajita Vegetables, Lettuce]]\n",
       "4621    Fresh Tomato Salsa, Fajita Vegetables, Pinto B...\n",
       "Name: choice_description, Length: 4622, dtype: object"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 福利\n",
    "\n",
    "orders.choice_description.str.replace('[','')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0                                                     NaN\n",
       "1                                              Clementine\n",
       "2                                                   Apple\n",
       "3                                                     NaN\n",
       "4       Tomatillo-Red Chili Salsa (Hot), Black Beans, ...\n",
       "                              ...                        \n",
       "4617    Fresh Tomato Salsa, Rice, Black Beans, Sour Cr...\n",
       "4618    Fresh Tomato Salsa, Rice, Sour Cream, Cheese, ...\n",
       "4619    Fresh Tomato Salsa, Fajita Vegetables, Pinto B...\n",
       "4620       Fresh Tomato Salsa, Fajita Vegetables, Lettuce\n",
       "4621    Fresh Tomato Salsa, Fajita Vegetables, Pinto B...\n",
       "Name: choice_description, Length: 4622, dtype: object"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "orders.choice_description.str.replace('[','').str.replace(']','')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "orders.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0                                                     NaN\n",
       "1                                              Clementine\n",
       "2                                                   Apple\n",
       "3                                                     NaN\n",
       "4       Tomatillo-Red Chili Salsa (Hot), Black Beans, ...\n",
       "                              ...                        \n",
       "4617    Fresh Tomato Salsa, Rice, Black Beans, Sour Cr...\n",
       "4618    Fresh Tomato Salsa, Rice, Sour Cream, Cheese, ...\n",
       "4619    Fresh Tomato Salsa, Fajita Vegetables, Pinto B...\n",
       "4620       Fresh Tomato Salsa, Fajita Vegetables, Lettuce\n",
       "4621    Fresh Tomato Salsa, Fajita Vegetables, Pinto B...\n",
       "Name: choice_description, Length: 4622, dtype: object"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "orders.choice_description.str.replace('[\\[\\]]','')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
